package com.hlk.admin.controller;

import com.hlk.admin.service.CategoryService;
import com.hlk.admin.CategoryMngControllerApi;
import com.hlk.enums.ResponseStatusEnum;
import com.hlk.pojo.Category;
import com.hlk.pojo.bo.SaveCategoryBO;
import com.hlk.result.Result;
import com.hlk.utils.ConvertUtils;
import com.hlk.utils.JsonUtils;
import com.hlk.utils.ValidateUtils;
import com.hlk.utils.redis.RedisKeys;
import com.hlk.utils.redis.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hlk
 * @version 1.0
 * @date 2020-12-29 14:01
 */
@RestController
public class CategoryMngController implements CategoryMngControllerApi {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Result saveOrUpdateCategory(@Valid SaveCategoryBO newCategoryBO, BindingResult result) {
        // 校验参数是否符合
        if (result.hasErrors()) {
            Map<String, String> map = ValidateUtils.checkParams(result);
            return Result.errorMap(map);
        }
        Category category = ConvertUtils.sourceToTarget(newCategoryBO, Category.class);
        // id为空新增，不为空修改
        if (newCategoryBO.getId() == null) {
            // 查询新增的分类名称不能重复存在
            boolean isExist = categoryService.selectCatIsExist(category.getName(), null);
            if (!isExist) {
                // 新增到数据库
                categoryService.createCategory(category);
            } else {
                return Result.errorCustom(ResponseStatusEnum.CATEGORY_EXIST_ERROR);
            }
        } else {
            // 查询修改的分类名称不能重复存在
            boolean isExist = categoryService.selectCatIsExist(category.getName(), newCategoryBO.getOldName());
            if (!isExist) {
                // 修改到数据库
                categoryService.modifyCategory(category);
            } else {
                return Result.errorCustom(ResponseStatusEnum.CATEGORY_EXIST_ERROR);
            }
        }
        return Result.ok();
    }

    @Override
    public Result getCatList() {
        List<Category> list = categoryService.selectAllCategory();
        return Result.ok(list);
    }

    @Override
    public Result getCats() {
        // 从缓存中获取分类json
        String categoryStr = redisUtils.get(RedisKeys.getCategoryKey());
        List<Category> categoryList = new ArrayList<>();
        if (StringUtils.isBlank(categoryStr)) {
            // 从数据库中获取
            List<Category> list = categoryService.selectAllCategory();
            // 放入缓存
            redisUtils.set(RedisKeys.getCategoryKey(), JsonUtils.objectToJson(list));
        } else {
            categoryList = JsonUtils.jsonToList(categoryStr, Category.class);
        }
        return Result.ok(categoryList);
    }
}
